package com.soul.doc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.soul.common.utils.ListUtil;
import com.soul.doc.bean.dto.LikeCommentDto;
import com.soul.doc.bean.po.LikeCommentPo;
import com.soul.doc.mapper.LikeCommentMapper;
import com.soul.doc.service.LikeCommentService;
import com.soul.common.service.BaseService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sws
 * @since 2023-12-16 21:53:18
 */
@Service
public class LikeCommentServiceImpl extends BaseService<LikeCommentMapper, LikeCommentPo> implements LikeCommentService {


    @Override
    public Map<Long,List<Long>> queryUserMap(List<Long> commentIds){
        Map<Long,List<Long>> likeMap = new HashMap<>();
        if (ObjectUtil.isNotEmpty(commentIds)){
            List<LikeCommentPo> list = this.getLambadaQueryWrapper().in(LikeCommentPo::getCommentId, commentIds).list();
            Map<Long, List<LikeCommentPo>> longListMap = ListUtil.listToMapList(list, t -> t.getCommentId());
            if (ObjectUtil.isNotEmpty(longListMap)){
                longListMap.forEach((k,listPo)->{
                    List<Long> userIds = ListUtil.toValueList(listPo, LikeCommentPo::getUserId);
                    likeMap.put(k,userIds);
                });
            }
        }
        return likeMap;
    }

    @Override
    public boolean addLikeComment(LikeCommentDto dto){
        LikeCommentPo one = this.getLambadaQueryWrapper().eq(LikeCommentPo::getCommentId, dto.getCommentId())
                .eq(LikeCommentPo::getUserId, dto.getUserId()).one();
        if (ObjectUtil.isNotEmpty(one)){
            if ("remove".equals(dto.getType())){
                this.removeById(one.getId());
                return true;
            }
            return false;
        }else {
            LikeCommentPo likeCommentPo = BeanUtil.copyProperties(dto, LikeCommentPo.class);
            this.save(likeCommentPo);
            return  true;
        }
    }
}
